<template>
	<view class="add-agent">
		<!-- <view class="step flex-center">
			<view :class="['box', 'flex-column', { active: stepCurrent >= stepIndex }]" v-for="(stepItem, stepIndex) in stepList" :key="stepIndex">
				<view class="icon-box flex-align-center">
					<view class="bg flex-center"><image :src="stepItem.icon" mode="aspectFill"></image></view>
					<view class="line" v-if="stepIndex < stepList.length - 1"></view>
				</view>
				<text :class="['text-name', { 'text-more': stepIndex == 1 }]">{{ stepItem.name }}</text>
			</view>
		</view> -->
		<view class="add-agent-contain">
			<view class="open-policy-contain-item">
				<u-form :model="form" ref="uForm" :error-type="errorType" :rules="rules">
					<view>

						<view class="checkBox-m">
							<view class="tip">
								政策:
							</view>
							<select-cy @click="clickMy" :value="form.policyName" slabel="policyName" svalue="policyName"
								:options="policyList" @change="policyChange"></select-cy>
						</view>
						<view class="checkBox-m" style="z-index: 1;">
							<view class="tip">
								交易类型:
							</view>
							<select-cy :value="form.totalTypeName" slabel="dictLabel" svalue="dictLabel"
								:options="totalTypeList" @change="totalTypeChange"></select-cy>
						</view>
						<u-form-item :border-bottom="false" class="form-item" label="分润截留占比(%)" prop="percent"
							label-width="300">
							<u-number-box input-width="200" long-press :positive-integer="false" v-model="form.percent"
								:step="1" :min="1" :max="100"></u-number-box>
						</u-form-item>

					</view>

				</u-form>
			</view>
		</view>
		<view class="button-box">

			<view class="next-btn button" @click="nextBtn">
				{{type=='add'?'确定新增':'确认修改'}}
			</view>
		</view>
		<!-- <u-picker v-model="isShowPreAgent" mode="selector"></u-picker> -->


	</view>
</template>

<script>
	import {
		addInfo,
		editInfo
	} from '@/api/price-property';

	export default {
		data() {
			return {
				form: {
					policyName: [],
					totalTypeName: [],
					percent: 1,
				},
				second: 0,
				disabled: false,
				stepCurrent: 0,
				rules: {
					percent: [{
						required: true,
						message: '请输入占比',
						trigger: 'blur'
					}],

					passwd: [{
						required: true,
						message: '请输入密码',
						trigger: 'blur'
					}],


				},
				errorType: ['toast'],

				isShowPreAgent: false,
				type: 'add',
				policyName: [],
				totalType: [],
			};
		},
		onLoad(options) {
			// checkPreAgent().then(res => {
			// 	this.switchFlag = res.data.switchFlag;
			// 	if (this.switchFlag == 1) {
			// 		this.getAgentFirst();
			// 	}
			// });
			this.type = options.type || 'add'
			this.$nextTick(() => {
				if (this.type == 'edit') {
					uni.setNavigationBarTitle({
						title: '修改'
					})
					let item = this.$storage.getStore("propertyDetailInfo") || {};
					console.log(item)
					this.form = item
					this.form.totalType = this.form.totalType.split(",");
					this.form.policyName = this.form.policyName.split(",");
					this.form.policyCode = this.form.policyCode.split(",");
					this.form.totalTypeName = []
					// 获取
					this.form.totalType.map((type) => {
						this.totalTypeList.map((item) => {
							if (item.dictValue == type) {
								console.log(item)
								this.form.totalTypeName.push(item.dictLabel);
							}
						});
					});

				}
			})

		},
		computed: {
			policyList() {
				return this.$store.getters.policyList || [];
			},
			totalTypeList() {
				return this.$store.getters.totalTypeList || [];
			},

		},
		methods: {
			totalTypeChange(v, c) {
				this.form.totalTypeName = c
				// console.log(v, this.form.totalType)
			},
			policyChange(v, c) {
				this.form.policyName = c
				// console.log(v, this.form.policyName)
			},
			// 下一步
			nextBtn() {
				let params = {
					...this.form
				};

				// this.stepCurrent += 1;

				console.log({
					params
				});
				if (!params.policyName.length || !params.totalTypeName.length) {
					return this.$tip.toast('选择项不能为空!')
				}
				let codeList = [],
					totalList = [];
				params.policyName.map((name) => {
					this.policyList.find((item) => {
						if (item.policyName == name) {
							codeList.push(item.policyCode);
						}
					});
				});
				params.totalTypeName.map((name) => {
					this.totalTypeList.find((item) => {
						if (item.dictLabel == name) {
							totalList.push(item.dictValue);
						}
					});
				});
				params.policyCode = codeList.join(",");
				params.policyName = params.policyName.join(",");
				params.totalType = totalList.join(',');
				params.percent = params.percent / 100;

				// return
				this.$tip.loading('新增中.....');
				// return;
				if (this.type == 'add') {
					addInfo(params)
						.then(response => {
							this.$tip.loaded();
							if (response.code === 200) {
								this.$tip.modal({
									content: response.msg
								}).then(res => {
									this.$nav.goBack();
									uni.$emit('update')
								})
							}
						})
						.catch(err => {
							this.$tip.loaded();
						});
				} else {
					editInfo(params)
						.then(response => {
							this.$tip.loaded();
							if (response.code === 200) {
								this.$tip.modal({
									title: '提示',
									content: response.msg,
									showCancel: false
								}).then(res => {
									this.$nav.goBack(2);
									uni.$emit('update')
								})
							}
						})
						.catch(err => {
							this.$tip.loaded();
						});
				}


			},
			// 上一步
			preBtn() {
				const index = this.stepCurrent;
				// this.stepCurrent = index == 0 ? 0 : index - 1;
				this.stepCurrent -= 1;
			}
		},
		onReady() {
			this.$refs.uForm.setRules(this.rules);
		}
	};
</script>

<style lang="scss" scoped>
	$activeColor: #fff;
	$noActiveColor: #bcdcff;

	page {
		background: #f6f9ff;
	}

	.add-agent {
		margin: R(30);
	}

	.add-agent-contain {
		border-radius: R(30);
		// overflow: hidden;
		position: relative;
		z-index: 1;
		background: #fff;
		padding: R(20) R(30);
	}

	.button-box {
		display: flex;
		justify-content: space-around;
		align-items: center;
		margin-top: R(50);

		.button {
			border-radius: R(50);
			padding: 0 R(30);
			background-color: $themeColor;
			text-align: center;
			height: R(80);
			line-height: R(80);
		}

		.pre-btn {
			width: 30%;
			background-color: #ffffff;
		}

		.next-btn {
			width: 60%;
			color: #ffffff;
		}
	}

	.checkBox-m {
		width: 100%;
		padding-bottom: 20upx;
		position: relative;

		.tip {
			padding: 10upx 0;
		}

		border-bottom: 1px solid #eee;
	}
</style>